﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.IDAL.FoodTesting;
using System.Data.SqlClient;
using System.Data;
using StoragePeriod.DBUtility;
using StoragePeriod.Model.FoodTesting;

namespace StoragePeriod.SqlServerDAL.FoodTesting
{
    /// <summary>
    /// 数据访问类:DietaryFiberData
    /// </summary>
    public partial class D_DietaryFiberData : I_DietaryFiberData
    {
        public D_DietaryFiberData()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from DietaryFiberData");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_DietaryFiberData model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DietaryFiberData(");
            strSql.Append("RawID,RawName,MBR1,MBR2,MPB,MAB,MB,MR1,MR2,MP,MA,M1,M2,Result,FoodTime,FoodNum)");
            strSql.Append(" values (");
            strSql.Append("@RawID,@RawName,@MBR1,@MBR2,@MPB,@MAB,@MB,@MR1,@MR2,@MP,@MA,@M1,@M2,@Result,@FoodTime,@FoodNum)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RawID", SqlDbType.Int,4),
					new SqlParameter("@RawName", SqlDbType.NVarChar,50),
					new SqlParameter("@MBR1", SqlDbType.NVarChar,50),
					new SqlParameter("@MBR2", SqlDbType.NVarChar,50),
					new SqlParameter("@MPB", SqlDbType.NVarChar,50),
					new SqlParameter("@MAB", SqlDbType.NVarChar,50),
					new SqlParameter("@MB", SqlDbType.NVarChar,50),
					new SqlParameter("@MR1", SqlDbType.NVarChar,50),
					new SqlParameter("@MR2", SqlDbType.NVarChar,50),
					new SqlParameter("@MP", SqlDbType.NVarChar,50),
					new SqlParameter("@MA", SqlDbType.NVarChar,50),
					new SqlParameter("@M1", SqlDbType.NVarChar,50),
					new SqlParameter("@M2", SqlDbType.NVarChar,50),
					new SqlParameter("@Result", SqlDbType.NVarChar,50),
					new SqlParameter("@FoodTime", SqlDbType.DateTime),
					new SqlParameter("@FoodNum", SqlDbType.Int,4)};
            parameters[0].Value = model.RawID;
            parameters[1].Value = model.RawName;
            parameters[2].Value = model.MBR1;
            parameters[3].Value = model.MBR2;
            parameters[4].Value = model.MPB;
            parameters[5].Value = model.MAB;
            parameters[6].Value = model.MB;
            parameters[7].Value = model.MR1;
            parameters[8].Value = model.MR2;
            parameters[9].Value = model.MP;
            parameters[10].Value = model.MA;
            parameters[11].Value = model.M1;
            parameters[12].Value = model.M2;
            parameters[13].Value = model.Result;
            parameters[14].Value = model.FoodTime;
            parameters[15].Value = model.FoodNum;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(E_DietaryFiberData model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DietaryFiberData set ");
            strSql.Append("RawID=@RawID,");
            strSql.Append("RawName=@RawName,");
            strSql.Append("MBR1=@MBR1,");
            strSql.Append("MBR2=@MBR2,");
            strSql.Append("MPB=@MPB,");
            strSql.Append("MAB=@MAB,");
            strSql.Append("MB=@MB,");
            strSql.Append("MR1=@MR1,");
            strSql.Append("MR2=@MR2,");
            strSql.Append("MP=@MP,");
            strSql.Append("MA=@MA,");
            strSql.Append("M1=@M1,");
            strSql.Append("M2=@M2,");
            strSql.Append("Result=@Result,");
            strSql.Append("FoodTime=@FoodTime,");
            strSql.Append("FoodNum=@FoodNum");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@RawID", SqlDbType.Int,4),
					new SqlParameter("@RawName", SqlDbType.NVarChar,50),
					new SqlParameter("@MBR1", SqlDbType.NVarChar,50),
					new SqlParameter("@MBR2", SqlDbType.NVarChar,50),
					new SqlParameter("@MPB", SqlDbType.NVarChar,50),
					new SqlParameter("@MAB", SqlDbType.NVarChar,50),
					new SqlParameter("@MB", SqlDbType.NVarChar,50),
					new SqlParameter("@MR1", SqlDbType.NVarChar,50),
					new SqlParameter("@MR2", SqlDbType.NVarChar,50),
					new SqlParameter("@MP", SqlDbType.NVarChar,50),
					new SqlParameter("@MA", SqlDbType.NVarChar,50),
					new SqlParameter("@M1", SqlDbType.NVarChar,50),
					new SqlParameter("@M2", SqlDbType.NVarChar,50),
					new SqlParameter("@Result", SqlDbType.NVarChar,50),
					new SqlParameter("@FoodTime", SqlDbType.DateTime),
					new SqlParameter("@FoodNum", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.RawID;
            parameters[1].Value = model.RawName;
            parameters[2].Value = model.MBR1;
            parameters[3].Value = model.MBR2;
            parameters[4].Value = model.MPB;
            parameters[5].Value = model.MAB;
            parameters[6].Value = model.MB;
            parameters[7].Value = model.MR1;
            parameters[8].Value = model.MR2;
            parameters[9].Value = model.MP;
            parameters[10].Value = model.MA;
            parameters[11].Value = model.M1;
            parameters[12].Value = model.M2;
            parameters[13].Value = model.Result;
            parameters[14].Value = model.FoodTime;
            parameters[15].Value = model.FoodNum;
            parameters[16].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DietaryFiberData ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DietaryFiberData ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public E_DietaryFiberData GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,RawID,RawName,MBR1,MBR2,MPB,MAB,MB,MR1,MR2,MP,MA,M1,M2,Result,FoodTime,FoodNum from DietaryFiberData ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            E_DietaryFiberData model = new E_DietaryFiberData();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RawID"].ToString() != "")
                {
                    model.RawID = int.Parse(ds.Tables[0].Rows[0]["RawID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RawName"] != null)
                {
                    model.RawName = ds.Tables[0].Rows[0]["RawName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MBR1"] != null)
                {
                    model.MBR1 = ds.Tables[0].Rows[0]["MBR1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MBR2"] != null)
                {
                    model.MBR2 = ds.Tables[0].Rows[0]["MBR2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MPB"] != null)
                {
                    model.MPB = ds.Tables[0].Rows[0]["MPB"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MAB"] != null)
                {
                    model.MAB = ds.Tables[0].Rows[0]["MAB"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MB"] != null)
                {
                    model.MB = ds.Tables[0].Rows[0]["MB"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MR1"] != null)
                {
                    model.MR1 = ds.Tables[0].Rows[0]["MR1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MR2"] != null)
                {
                    model.MR2 = ds.Tables[0].Rows[0]["MR2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MP"] != null)
                {
                    model.MP = ds.Tables[0].Rows[0]["MP"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MA"] != null)
                {
                    model.MA = ds.Tables[0].Rows[0]["MA"].ToString();
                }
                if (ds.Tables[0].Rows[0]["M1"] != null)
                {
                    model.M1 = ds.Tables[0].Rows[0]["M1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["M2"] != null)
                {
                    model.M2 = ds.Tables[0].Rows[0]["M2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Result"] != null)
                {
                    model.Result = ds.Tables[0].Rows[0]["Result"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FoodTime"].ToString() != "")
                {
                    model.FoodTime = DateTime.Parse(ds.Tables[0].Rows[0]["FoodTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FoodNum"].ToString() != "")
                {
                    model.FoodNum = int.Parse(ds.Tables[0].Rows[0]["FoodNum"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,RawID,RawName,MBR1,MBR2,MPB,MAB,MB,MR1,MR2,MP,MA,M1,M2,Result,FoodTime,FoodNum ");
            strSql.Append(" FROM DietaryFiberData ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,RawID,RawName,MBR1,MBR2,MPB,MAB,MB,MR1,MR2,MP,MA,M1,M2,Result,FoodTime,FoodNum ");
            strSql.Append(" FROM DietaryFiberData ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "DietaryFiberData";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        /// <summary>
        /// 根据原料ID和检测时间 删除检测记录
        /// </summary>
        /// <param name="eVcData">要删除的检测记录</param>
        /// <returns></returns>
        public int RemoveByTime(E_DietaryFiberData eE_DietaryFiberData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DietaryFiberData where RawID=" + eE_DietaryFiberData.RawID + " and FoodTime=CAST('" + eE_DietaryFiberData.FoodTime.ToString() + "' as datetime)");
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除某一次检测记录
        /// </summary>
        /// <param name="eVcData">要删除的检测记录</param>
        /// <returns></returns>
        public int RemoveByFoodNum(E_DietaryFiberData eE_DietaryFiberData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DietaryFiberData where RawID=" + eE_DietaryFiberData.RawID + " and FoodTime=CAST('" + eE_DietaryFiberData.FoodTime.ToString() + "' as datetime) and FoodNum=" + eE_DietaryFiberData.FoodNum);
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        #endregion  Method
    }
}
